import pygame
import pygame_gui
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from utils import get_chinese_font

class MainMenu:
    """游戏主菜单"""
    def __init__(self, window):
        self.window = window
        self.width, self.height = window.get_width(), window.get_height()
        self.background_color = (139, 69, 19)  # 保持原有的棕色背景
        
        # 创建pygame_gui管理器
        self.manager = pygame_gui.UIManager((self.width, self.height))
        
        # 时钟
        self.clock = pygame.time.Clock()
        
        # 确保中文显示正常
        self.title_font = get_chinese_font(48)
        self.small_font = get_chinese_font(24)
        
        # 创建标题
        self.title = self.title_font.render("三国杀", True, (255, 215, 0))
        self.title_rect = self.title.get_rect(center=(self.width//2, 150))
        
        # 创建按钮
        self.start_button = pygame_gui.elements.UIButton(
            relative_rect=pygame.Rect((self.width//2 - 100, 300), (200, 50)),
            text='开始游戏',
            manager=self.manager
        )
        
        self.exit_button = pygame_gui.elements.UIButton(
            relative_rect=pygame.Rect((self.width//2 - 100, 380), (200, 50)),
            text='退出游戏',
            manager=self.manager
        )
        
        # 创建背景装饰
        self.background_pattern = self._create_background_pattern()
    
    def _create_background_pattern(self):
        """创建背景装饰图案"""
        # 创建一个透明表面用于绘制背景图案
        pattern = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
        # 添加一些简单的装饰元素
        for i in range(0, self.width, 50):
            for j in range(0, self.height, 50):
                pygame.draw.circle(pattern, (255, 255, 255, 20), (i, j), 2)
        return pattern
    
    def handle_event(self, event: pygame.event.Event) -> str:
        """处理菜单事件"""
        self.manager.process_events(event)
        
        if event.type == pygame.QUIT:
            return "exit"
            
        if event.type == pygame.USEREVENT:
            if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
                if event.ui_element == self.start_button:
                    return "start_game"
                elif event.ui_element == self.exit_button:
                    return "exit"
        
        return None
    
    def render(self):
        """渲染菜单"""
        # 填充背景
        self.window.fill(self.background_color)
        
        # 绘制背景图案
        self.window.blit(self.background_pattern, (0, 0))
        
        # 绘制标题
        self.window.blit(self.title, self.title_rect)
        
        # 绘制副标题
        subtitle = self.small_font.render("卡牌对战游戏", True, (255, 255, 255))
        subtitle_rect = subtitle.get_rect(center=(self.width//2, 220))
        self.window.blit(subtitle, subtitle_rect)
        
        # 绘制按钮
        self.manager.draw_ui(self.window)
        
        # 更新UI
        self.manager.update(self.clock.tick(60) / 1000.0)
        
    def get_manager(self):
        """获取pygame_gui管理器"""
        return self.manager